package com.controller;




import com.model.auto.CollectInfo;
import com.model.auto.SchoolInfoV2;
import com.model.auto.vo.GetSchoolV2Vo;
import com.service.ICollectService;
import com.service.ISchoolInfoV2Service;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * 新院校信息
 *
 * @author zhaoyifan
 */
@RestController
@RequestMapping("/schoolInfoV2")
public class SchoolInfoV2Controller extends BaseController{

    @Autowired
    private ISchoolInfoV2Service schoolInfoService;
    @Autowired
    private ICollectService collectService;

    /**
     * 手机端院校查询
     * @param schoolVo
     * @return
     */
    @PostMapping("mobile/getSchoolInfoPage")
    public R getSchoolInfoPage(@RequestBody GetSchoolV2Vo schoolVo, HttpServletRequest request) {
        Long userId = getUserId(request);
        schoolVo.setPageNum((schoolVo.getPageNum()-1)*schoolVo.getPageSize());
        covertRequestParams(schoolVo);
        List<SchoolInfoV2> result = schoolInfoService.queryByParams(schoolVo);
        //获取是否收藏
        if(Objects.nonNull(userId)){
            for(SchoolInfoV2 schoolInfoV2:result){
                schoolInfoV2.setCollectFlag(collectService.isCollect(new CollectInfo(userId, schoolInfoV2.getCode(), 1)));
            }
        }
        Integer total = schoolInfoService.queryByTotalParams(schoolVo);
        R r = R.ok(result);
        r.put("total", total);
        return r;
    }

    private void covertRequestParams(GetSchoolV2Vo schoolVo) {
        if (!CollectionUtils.isEmpty(schoolVo.getQualifications())) {
            List<String> returnList = new ArrayList<>();
            Map<String, String> qualificationsMap = new HashMap<>();
            qualificationsMap.put("1", "硕士点");
            qualificationsMap.put("2", "博士点");
            qualificationsMap.put("3", "研究生院");
            qualificationsMap.put("4", "自划线");
            qualificationsMap.put("5", "推免资格");

            for (String qualificationsStr : schoolVo.getQualifications()) {
                returnList.add(qualificationsMap.get(qualificationsStr));
            }
            schoolVo.setQualificationsStr(String.join("|",returnList));
        }
        if (!CollectionUtils.isEmpty(schoolVo.getHierarchys())) {
            List<String> returnList = new ArrayList<>();
            Map<String, String> collegeLevelMap = new HashMap<>();
            collegeLevelMap.put("1", "双一流");
            collegeLevelMap.put("2", "中央直管");
            collegeLevelMap.put("3", "985");
            collegeLevelMap.put("4", "211");
            collegeLevelMap.put("5", "双高计划");
            collegeLevelMap.put("6", "国家示范(骨干)");

            for (String collegeLevelStr : schoolVo.getHierarchys()) {
                returnList.add(collegeLevelMap.get(collegeLevelStr));
            }
            schoolVo.setHierarchysStr(String.join("|",returnList));
        }

        if (!CollectionUtils.isEmpty(schoolVo.getNature())) {
            List<String> returnList = new ArrayList<>();
            Map<String, String> natureMap = new HashMap<>();
            natureMap.put("1", "公办");
            natureMap.put("2", "民办");
            natureMap.put("3", "合办");
            for (String qualificationsStr : schoolVo.getNature()) {
                returnList.add(natureMap.get(qualificationsStr));
            }
            schoolVo.setNature(returnList);
        }

    }
}
